<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-09-05
 * Time: 12:12
 */

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\User;
use App\Models\UserH5;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class UserLogin extends Controller
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @param string|null $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $accessToken = $request->header("accessToken");
        $user = User::where("accessToken", $accessToken)->first();
        if (!$user) {
            return $this->apiResponse('', config('errorCode.PERMISSION_DENY'));
        }
        User::where("accessToken", $accessToken)->update(['onlineTime' => date("Y-m-d H:i:s")]);
        if ($user['logoutStatus'] || $user['userLockStatus']) {
            return $this->apiResponse("", [
                'errorCode' => '30002',
                'errorMessage' => "用户已被禁用,如有疑问,请联系工作人员"
            ]);
        }
        //\RedisDB::set($user['userId'],$accessToken);
        $request->USERID = $user['userId'];
        $request->MEMBERSHIPID = $user['membershipId'];
        $request->ISCONTRACT = $user['isContract'];
        $request->MYTEAMID = $user['myTeamId'];
        $request->PARTITIONID = $user['partitionId'];
        $request->LEVEL = $user['level'];
        $request->BALANCE = $user['balance'];
        $pageSize = $request->pageSize ? $request->pageSize : 20;
        return $next($request);

    }

}

